package com.hui.account.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;

import androidx.annotation.NonNull;

import com.hui.account.R;
import com.hui.account.utils.ToastUtils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 时间选择器
 */
public class DateDialog extends Dialog implements View.OnClickListener {

    DatePicker datePicker; // 日历

    EditText hourEdit, minuteEdit; // 时间

    Button cancelBtn, ensureBtn; // 按钮

    public interface OnEnsureListener {
        public void onEnsure(String time);
    }

    OnEnsureListener onEnsureListener;

    public void setOnEnsureListener(OnEnsureListener onEnsureListener) {
        this.onEnsureListener = onEnsureListener;
    }

    public DateDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_date);

        // 1、初始化页面
        initView();

        // 2、隐藏日历顶部页面
        hideDatePickerHeader();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.dialog_time_btn_cancel:
                cancel();
                break;
            case R.id.dialog_time_btn_ensure:
                // 小时/分钟 数值校验
                int hour = 0;
                int minute = 0;
                String hours = hourEdit.getText().toString();
                String minutes = minuteEdit.getText().toString();
                if (TextUtils.isEmpty(hours)){
                    cancel();
                    return;
                }

                if (TextUtils.isEmpty(minutes)) {
                    ToastUtils.error(getContext(), "【必填项】分钟不能为空", Gravity.BOTTOM, 0, 100);
                    return;
                }

                hour = Integer.parseInt(hours);
                if (hour > 23) {
                    ToastUtils.error(getContext(), "小时输入有误！", Gravity.BOTTOM, 0, 100);
                    return;
                }

                minute = Integer.parseInt(minutes);
                if (minute > 59) {
                    ToastUtils.error(getContext(), "分钟输入有误", Gravity.BOTTOM, 0, 100);
                    return;
                }

                hours = String.valueOf(hour);
                minutes = String.valueOf(minute);
                if (hour < 10)
                    hours = "0" + hour;

                if (minute < 10)
                    minutes = "0" + minute;

                // 获取年月日时分，并进行特殊值处理
                int year = datePicker.getYear();
                int month = datePicker.getMonth() + 1;
                int day = datePicker.getDayOfMonth();

                String months = String.valueOf(month);
                if (month < 10)
                    months = "0" + month;

                String days = String.valueOf(day);
                if (day < 10)
                    days = "0" + day;

                String time = year + "年" + months + "月" + days + "日 " + hours + ":" + minutes;

                if (null != onEnsureListener) {
                    onEnsureListener.onEnsure(time);
                }

                cancel();
                break;
        }
    }

    /**
     * 初始化页面
     */
    private void initView() {
        datePicker = findViewById(R.id.dialog_date);
        cancelBtn = findViewById(R.id.dialog_time_btn_cancel);
        ensureBtn = findViewById(R.id.dialog_time_btn_ensure);
        hourEdit = findViewById(R.id.dialog_date_hour);
        minuteEdit = findViewById(R.id.dialog_date_minute);

        cancelBtn.setOnClickListener(this);
        ensureBtn.setOnClickListener(this);
    }

    /**
     * 隐藏日历顶部页面
     */
    private void hideDatePickerHeader() {
        ViewGroup rootView = (ViewGroup) datePicker.getChildAt(0);
        if (null == rootView)
            return;

        View headerView = rootView.getChildAt(0);
        if (null == headerView)
            return;

        // 5.0+
        int headerId = getContext().getResources().getIdentifier("day_picker_selector_layout", "id", "android");
        if (headerId == headerView.getId()) {
            headerView.setVisibility(View.GONE);
            ViewGroup.LayoutParams layoutParams = rootView.getLayoutParams();
            layoutParams.width = ViewGroup.LayoutParams.WRAP_CONTENT;
            rootView.setLayoutParams(layoutParams);

            ViewGroup animator = (ViewGroup) datePicker.getChildAt(1);
            ViewGroup.LayoutParams layoutParamsAnimator = animator.getLayoutParams();
            layoutParamsAnimator.width = ViewGroup.LayoutParams.WRAP_CONTENT;
            animator.setLayoutParams(layoutParamsAnimator);

            View child = animator.getChildAt(0);
            ViewGroup.LayoutParams layoutParamsChild = child.getLayoutParams();
            layoutParamsChild.width = ViewGroup.LayoutParams.WRAP_CONTENT;
            child.setLayoutParams(layoutParamsChild);
            return;
        }

        // 6.0+
        headerId = getContext().getResources().getIdentifier("date_picker_header", "id", "android");
        if (headerId == headerView.getId()) {
            headerView.setVisibility(View.GONE);
        }
    }
}
